package com.erplan.studentmgt.desktop.elements;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.MessageBox;

import com.erplan.studentmgt.desktop.StudentMgt;

public class AppMenu {

	public static void init(){
		if(StudentMgt.shell.getMenu() != null){
			(StudentMgt.shell.getMenu()).dispose();
		}
		if(StudentMgt.menuBar != null){
			StudentMgt.menuBar.dispose();
		}

		//=====================================================================
		//Creating Menu bar
		//=====================================================================
		StudentMgt.menuBar = new Menu(StudentMgt.shell, SWT.BAR);
		StudentMgt.shell.setMenuBar(StudentMgt.menuBar);

		//=====================================================================
		// Declare Menu Item
		//=====================================================================
		MenuItem fileItem = new MenuItem(StudentMgt.menuBar, SWT.CASCADE);
		fileItem.setText("File");
		fileItem.addListener(SWT.Arm, AppStatusBar.armListener);
		MenuItem editItem = new MenuItem(StudentMgt.menuBar, SWT.CASCADE);
		editItem.setText("Edit");
		editItem.addListener(SWT.Arm, AppStatusBar.armListener);
		MenuItem viewItem = new MenuItem(StudentMgt.menuBar, SWT.CASCADE);
		viewItem.setText("View");
		viewItem.addListener(SWT.Arm, AppStatusBar.armListener);
		MenuItem helpItem = new MenuItem(StudentMgt.menuBar, SWT.CASCADE);
		helpItem.setText("Help");
		helpItem.addListener(SWT.Arm, AppStatusBar.armListener);

		//=====================================================================
		// Creating File Menu
		//=====================================================================
		{
			Menu fileMenu = new Menu(StudentMgt.menuBar);
			fileItem.setMenu(fileMenu);
			fileMenu.addListener(SWT.Hide, AppStatusBar.hideListener);
			fileMenu.addListener(SWT.Show, AppStatusBar.showListener);

			MenuItem newItem = new MenuItem(fileMenu, SWT.NONE);
			newItem.setText("&New\tCTRL+N");
			newItem.addListener(SWT.Arm, AppStatusBar.armListener);

			MenuItem openItem = new MenuItem(fileMenu, SWT.NONE);
			openItem.setText("&Open\tCTRL+O");
			openItem.setAccelerator(SWT.CTRL + 'O');
			openItem.addListener(SWT.Arm, AppStatusBar.armListener);

			MenuItem saveItem = new MenuItem(fileMenu, SWT.NONE);
			saveItem.setText("&Save\tCTRL+S");
			saveItem.addListener(SWT.Arm, AppStatusBar.armListener);

			MenuItem saveAsItem = new MenuItem(fileMenu, SWT.NONE);
			saveAsItem.setText("Save As...");
			saveAsItem.addListener(SWT.Arm, AppStatusBar.armListener);

			new MenuItem(fileMenu, SWT.SEPARATOR);
			MenuItem pageSetupItem = new MenuItem(fileMenu, SWT.NONE);
			pageSetupItem.setText("Page Setup...");
			pageSetupItem.addListener(SWT.Arm, AppStatusBar.armListener);

			//=====================================================================
			// Creating Sub Menu Of Print Menu
			//=====================================================================
			MenuItem subMenuIndex = new MenuItem(fileMenu, SWT.CASCADE);
		    subMenuIndex.setText("Print");
		    subMenuIndex.addListener(SWT.Arm, AppStatusBar.armListener);
		    {
			    Menu subMenu = new Menu(StudentMgt.menuBar);
			    subMenuIndex.setMenu(subMenu);
			    subMenu.addListener(SWT.Hide, AppStatusBar.hideListener);
			    subMenu.addListener(SWT.Show, AppStatusBar.showListener);

			    MenuItem subMenuItem = new MenuItem(subMenu, SWT.NONE);
			    subMenuItem.setText("Print");
			    subMenuItem.addListener(SWT.Arm, AppStatusBar.armListener);

			    subMenuItem = new MenuItem(subMenu, SWT.NONE);
			    subMenuItem.setText("Quick Print");
			    subMenuItem.addListener(SWT.Arm, AppStatusBar.armListener);
		    }

			new MenuItem(fileMenu, SWT.SEPARATOR);

			MenuItem exitItem = new MenuItem(fileMenu, SWT.NONE);
			exitItem.setText("Exit\tCTRL+X");
			exitItem.addListener(SWT.Arm, AppStatusBar.armListener);
			exitItem.setAccelerator (SWT.ALT + 'X');

			//Action Listeners - File Open Dialog Box
			class Open implements SelectionListener {
				public void widgetSelected(SelectionEvent event) {
					FileDialog fd = new FileDialog(StudentMgt.shell, SWT.OPEN);
					fd.setText("Open");
					fd.setFilterPath("C:/");
					String[] filterExt = { "*.txt", "*.doc", ".rtf", "*.*", "*.pdf" };
					fd.setFilterExtensions(filterExt);
					String selected = fd.open();
					System.out.println(selected);
				}

				public void widgetDefaultSelected(SelectionEvent event) {
				}
			}

			//Action Listeners - File Save Dialog Box
			class Save implements SelectionListener {
				public void widgetSelected(SelectionEvent event) {
					FileDialog fd = new FileDialog(StudentMgt.shell, SWT.SAVE);
					fd.setText("Save");
					fd.setFilterPath("C:/");
					String[] filterExt = { "*.txt", "*.doc", ".rtf", "*.*" };
					fd.setFilterExtensions(filterExt);
					String selected = fd.open();
					System.out.println(selected);
				}

				public void widgetDefaultSelected(SelectionEvent event) {
				}
			}
			openItem.addSelectionListener(new Open());
			saveItem.addSelectionListener(new Save());

			//Action Listeners - File Exit Dialog Box
			exitItem.addSelectionListener(new SelectionAdapter() {
				public void widgetSelected(SelectionEvent e) {
					MessageBox messageBox = new MessageBox(StudentMgt.shell, SWT.ICON_QUESTION | SWT.YES | SWT.NO);
					messageBox.setMessage("Do you really want to exit?");
					messageBox.setText("Exiting Application");
					int response = messageBox.open();
					if (response == SWT.YES)
						System.exit(0);
				}
			});
		}

		//=====================================================================
		// Creating EDIT Menu
		//=====================================================================
		Menu editMenu = new Menu(StudentMgt.menuBar);
		editItem.setMenu(editMenu);
		editMenu.addListener(SWT.Hide, AppStatusBar.hideListener);
		editMenu.addListener(SWT.Show, AppStatusBar.showListener);
		{
			MenuItem cutItem = new MenuItem(editMenu, SWT.NONE);
			cutItem.setText("Cut");
			cutItem.addListener(SWT.Arm, AppStatusBar.armListener);

			MenuItem pasteItem = new MenuItem(editMenu, SWT.NONE);
			pasteItem.setText("Paste");
			pasteItem.addListener(SWT.Arm, AppStatusBar.armListener);
		}

		//=====================================================================
		// Creating VIEW Menu
		//=====================================================================
		Menu viewMenu = new Menu(StudentMgt.menuBar);
		viewItem.setMenu(viewMenu);
		viewMenu.addListener(SWT.Hide, AppStatusBar.hideListener);
		viewMenu.addListener(SWT.Show, AppStatusBar.showListener);
		{
		    MenuItem toolItem = new MenuItem(viewMenu, SWT.NONE);
			toolItem.setText("ToolBars");
			toolItem.addListener(SWT.Arm, AppStatusBar.armListener);
			MenuItem fontItem = new MenuItem(viewMenu, SWT.NONE);
			fontItem.setText("Font");
			fontItem.addListener(SWT.Arm, AppStatusBar.armListener);
		}

		//=====================================================================
		// Creaing Help Menu
		//=====================================================================
		Menu helpMenu = new Menu(StudentMgt.menuBar);
		helpItem.setMenu(helpMenu);
		helpMenu.addListener(SWT.Hide, AppStatusBar.hideListener);
		helpMenu.addListener(SWT.Show, AppStatusBar.showListener);
		{
		    MenuItem aboutItem = new MenuItem(helpMenu, SWT.NONE);
			aboutItem.setText("About");
			aboutItem.addListener(SWT.Arm, AppStatusBar.armListener);
			aboutItem.addListener(SWT.Selection, new Listener () {
				public void handleEvent (Event e) {
					AboutApp.createAboutDialog(StudentMgt.shell, StudentMgt.display);
				}
			});
		}
	}
}